<?php
	####################################################
	# request 파라메터 클린                            #
	####################################################

	#	example)
	#		echo 'request post value >>';
	#		echo $_POST['test'];
	#
	#		$test_var = Request::clean_request_playload('test');
	#		echo "post value cleaned >> {$test_var}";
	#
	#		$req_var = Request::get_variable('test');
	#		echo "request value cleaned>>".$req_var;
	#
	#		$req_var = Request::get_variables_name();
	#
	#		$test_var = Request::clean_individual($_POST['test']);
	#		echo "post value cleaned >> {$test_var}";

	class Request {

		#폼 어레이로 받은 값의 배열을 정돈한다.
		public static function dic($form_array, $unique_check) {
			$retarr = array();

			# 유니크 키값이 있는 값만 정돈한다.
			foreach($form_array as $key=>$val){
				if(array_key_exists($unique_check, $form_array[$key])){
					$retarr[] = Request::clean_request($val);
				}
			}
			//Nlog::puts('$retarr', $retarr);
			return $retarr;
		}

		# do you know request header, used to clean_request_playload member function
		public static function clean_request_playload($var_name, $type = 'post', $mysql_enabled = FALSE){
			//echo "#1";
			if(isset($type) && $type == 'post'){
				$type = $_POST;
			} else if(isset($type) && $type == 'get') {
				$type = $_GET;
			} else if(isset($type) && $type == 'files'){
				$type = $_FILES;
			} else if(isset($type) && $type == 'cookie'){
				$type = $_COOKIE;
			} else {
				return $var_name;
			}
			//echo "#var_name";
			//print_r($var_name);
			//echo "#type";
			//print_r($type);
			if(array_key_exists($var_name, $type)){
				//echo "#2";
				$request_var = isset($type[$var_name]) ? $type[$var_name] : NULL;
				$request_var = htmlspecialchars($request_var);
				$request_var = strip_tags($request_var);
				$request_var = trim($request_var);
				if(get_magic_quotes_gpc()){
					$request_var = stripslashes($request_var);
				} else {
					$request_var = addslashes($request_var);
				}
				if($mysql_enabled == TRUE){
					$request_var = mysql_real_escape_string($request_var);
				}
				return $request_var;
			} else {
				return null;
			}
		}

		public static function get_variable($var_name) {
			$tmp_var = Request::clean_request_playload($var_name);
			return $tmp_var;
		}

		# display request variables
		public static function get_variables_name(){
			$arr = array("POST"=>$_POST, "GET"=>$_GET, "FILE"=>$_FILE, "COOKIE"=>$_COOKIE);
			echo "<pre>";
			var_dump($arr);
			echo "</pre>";
		}

		# simple individual variable only
		public static function clean_individual($get_or_post_variable_argument) {
			$request_var = $get_or_post_variable_argument;
			$request_var = htmlspecialchars($request_var);
			$request_var = strip_tags($request_var);
			$request_var = trim($request_var);
			$request_var = addslashes($request_var);
			$request_var = htmlentities($request_var, ENT_QUOTES, "UTF-8");
			return $request_var;
		}

		# array cleaning make new array
		#	example)
		#		$r_request = Request::clean_request($_REQUEST);
		public static function clean_request($request, $print_request = FALSE) {
			$ret_arr = array();
			foreach($request as $key=>$val){
				if(is_array($val)){
					foreach($val as $key1=>$val1){
						$val1 = Request::clean_individual($val1);
						$ret_arr[$key][] = $val1;
					}
				} else {
					$ret_arr[$key] = Request::clean_individual($val);
				}
			}
			if($print_request){
				Nlog::puts('req', $ret_arr);
			}
			return $ret_arr;
		}

		# 레퍼러 체크
		public static function referer() {
			//Nlog::puts('host', $_SERVER['HTTP_HOST'] );
			//Nlog::puts('ref',  $_SERVER['HTTP_REFERER'] );
			//if(!preg_match( sprintf("/%s/", $_SERVER['HTTP_HOST']), $_SERVER['HTTP_REFERER'])) exit("잘못된 접근");
			if(!preg_match( sprintf("/%s/", DOMAIN), getenv("HTTP_REFERER"))) exit("잘못된 접근");
			//$referer = getenv("HTTP_REFERER");
			//if(!eregi(DOMAIN, $referer)){ exit("error"); }
		}
	}
?>